package com.jeremyfeinstein.utils;

import android.content.Context;
import android.net.TrafficStats;

public class NetRxSpeedUtil {
	private static long lastTotalRxBytes = 0;
	private static long lastTimeStamp = 0;

	public static long getRxNetSpeed(Context mContext) {
		long nowTotalRxBytes = getTotalRxBytes(mContext);
		long nowTimeStamp = System.currentTimeMillis();
		// 毫秒转换
		long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / 4000);
		lastTimeStamp = nowTimeStamp;
		lastTotalRxBytes = nowTotalRxBytes;

		return speed;
	}


	private static long getTotalRxBytes(Context mContext) {
		//注 这里取到的是总的接收的字节数（下行）
		// 转为KB
		return TrafficStats.getUidRxBytes(mContext.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0
				: (TrafficStats.getTotalRxBytes() / 1024);//转为KB
	}

}
